﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.Utils;
using DevExpress.XtraCharts;
using DevExpress.XtraEditors;

namespace Client
{
    public partial class FrmMsg : BaseChildForm
    {
        public FrmMsg()
        {
            InitializeComponent();
            dateSpan.StartDateEdit.Properties.DisplayFormat.FormatString = "yyyy-MM";
            dateSpan.StartDateEdit.Properties.DisplayFormat.FormatType = FormatType.DateTime;
            dateSpan.EndDateEdit.Properties.DisplayFormat.FormatString = "yyyy-MM";
            dateSpan.EndDateEdit.Properties.DisplayFormat.FormatType = FormatType.DateTime;


            dateSpan.StartDateEdit.DateTime = DateTime.Now.AddMonths(-6).Date;
            dateSpan.EndDateEdit.DateTime = DateTime.Now.Date;
        }
        public override void BindData()
        {
            DrawChart();
        }
        private void FrmBusiness_Load(object sender, EventArgs e)
        {
            DrawChart();
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            DrawChart();
        }

        private void DrawChart()
        {
            if (this.dateSpan.StartDateEdit.EditValue == null||this.dateSpan.EndDateEdit.EditValue== null)
            {
                GlobalInfo.MsgBoxShowInfo("请选择统计日期段!");
                return;
            }

            DateTime startDate = dateSpan.StartDateEdit.DateTime;
            DateTime endDate = dateSpan.EndDateEdit.DateTime;
            QunYing.BLL.LeaveMsg msg = new QunYing.BLL.LeaveMsg();
            DataSet ds = msg.QueryTotalMsg(GlobalInfo.UserLoginInfo.用户ID, startDate, endDate);

            chartControl1.BeginInit();
            chartControl1.Series.Clear();
            Series series1 = new Series("series1", ViewType.Bar);
            series1.LegendText = "咨询数量";
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                series1.Points.Add(new SeriesPoint(ds.Tables[0].Rows[i]["月份"], ds.Tables[0].Rows[i]["咨询数量"]));
            }
            Series series2 = new Series("series2", ViewType.Bar);
            series2.LegendText = "建议数量";
            for (int i = 0; i < ds.Tables[1].Rows.Count; i++)
            {
                series2.Points.Add(new SeriesPoint(ds.Tables[1].Rows[i]["月份"], ds.Tables[1].Rows[i]["建议数量"]));
            }
            Series series3 = new Series("series3", ViewType.Bar);
            series3.LegendText = "投诉数量";
            for (int i = 0; i < ds.Tables[2].Rows.Count; i++)
            {
                series3.Points.Add(new SeriesPoint(ds.Tables[2].Rows[i]["月份"], ds.Tables[2].Rows[i]["投诉数量"]));
            }
            Series series4 = new Series("series4", ViewType.Bar);
            series4.LegendText = "好评数量";
            for (int i = 0; i < ds.Tables[3].Rows.Count; i++)
            {
                series4.Points.Add(new SeriesPoint(ds.Tables[3].Rows[i]["月份"], ds.Tables[3].Rows[i]["好评数量"]));
            }
            chartControl1.Series.Add(series1);
            chartControl1.Series.Add(series2);
            chartControl1.Series.Add(series3);
            chartControl1.Series.Add(series4);
            chartControl1.EndInit();
            ShowHide();
        }

        private void cbx_CheckedChanged(object sender, EventArgs e)
        {
            if (chartControl1.Series.Count < 4)
            {
                return;
            }
            ShowHide();
        }

        private void ShowHide()
        {
            chartControl1.Series[0].Visible = cbx1.Checked;
            chartControl1.Series[1].Visible = cbx2.Checked;
            chartControl1.Series[2].Visible = cbx3.Checked;
            chartControl1.Series[3].Visible = cbx4.Checked;
        }
    }
}